<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="DFU : Device Firmware Update 即固件升级      DFU可分为两种:dual bank   : 先接收后升级.   优点：升级失败可回滚  缺点:需要额外空间single bank : 边接收边升级.    优点: 节约空间   缺点:如果出错不能回滚版本     最新SDK(v15.3.0)会根据当前代码空间,自动选择使用dual bank或single bankN">
<meta property="og:type" content="article">
<meta property="og:title" content="nRF52832开发记录-DFU">
<meta property="og:url" content="http://example.com/2020/07/15/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-dfu/index.html">
<meta property="og:site_name" content="webuildthings">
<meta property="og:description" content="DFU : Device Firmware Update 即固件升级      DFU可分为两种:dual bank   : 先接收后升级.   优点：升级失败可回滚  缺点:需要额外空间single bank : 边接收边升级.    优点: 节约空间   缺点:如果出错不能回滚版本     最新SDK(v15.3.0)会根据当前代码空间,自动选择使用dual bank或single bankN">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-07-15T11:36:12.000Z">
<meta property="article:modified_time" content="2020-09-03T03:11:43.569Z">
<meta property="article:author" content="wusk">
<meta property="article:tag" content="BLE">
<meta property="article:tag" content="Nordic">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2020/07/15/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-dfu/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>nRF52832开发记录-DFU | webuildthings</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">webuildthings</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">遇酒且呵呵 人生能几何</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/07/15/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-dfu/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="wusk">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="webuildthings">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          nRF52832开发记录-DFU
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-07-15 19:36:12" itemprop="dateCreated datePublished" datetime="2020-07-15T19:36:12+08:00">2020-07-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-09-03 11:11:43" itemprop="dateModified" datetime="2020-09-03T11:11:43+08:00">2020-09-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/nRF52832/" itemprop="url" rel="index"><span itemprop="name">nRF52832</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/nRF52832/BLE/" itemprop="url" rel="index"><span itemprop="name">BLE</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>DFU : Device Firmware Update 即固件升级     </p>
<p>DFU可分为两种:<br><code>dual bank</code>   : 先接收后升级.   优点：升级失败可回滚  缺点:需要额外空间<br><code>single bank</code> : 边接收边升级.    优点: 节约空间   缺点:如果出错不能回滚版本    </p>
<p>最新SDK(v15.3.0)会根据当前代码空间,自动选择使用<code>dual bank</code>或<code>single bank</code><br>Nordic 支持三种升级方式: UART, USB, BLE    </p>
<p>Nordic要实现DFU需要两个独立的代码:<code>bootloader</code> 和 <code>app</code><br>下面为使用流程:     </p>
<a id="more"></a>


<h1 id="1-Bootloader"><a href="#1-Bootloader" class="headerlink" title="1. Bootloader"></a>1. Bootloader</h1><p>Nordic有两种bootloader: 开放式和安全式<br>开放式不做安全校验<br>安全式要做安全校验，只有校验通过的才生效    </p>
<h2 id="1-1-生成micro-ecc算法库"><a href="#1-1-生成micro-ecc算法库" class="headerlink" title="1.1 生成micro-ecc算法库"></a>1.1 生成micro-ecc算法库</h2><p>加密bootloader会使用到<code>micro-ecc</code>算法，这需要我们手动编译并生成<code>micro-ecc</code>算法库<br>确保本机装有git和GCC<br>进入文件夹<code>SDK根目录\external\micro-ecc</code>运行<code>build_all</code>脚本即可自动生成<code>micro-ecc</code>算法库</p>
<h2 id="1-2-生成公钥及私钥"><a href="#1-2-生成公钥及私钥" class="headerlink" title="1.2 生成公钥及私钥"></a>1.2 生成公钥及私钥</h2><p>例程位置: <code>SDK根目录\examples\dfu\secure_bootloader</code><br>安全式bootloader需要Key<br>私钥生成命令：<code>nrfutil keys generate priv.pem</code><br>公钥生成命令：<code>nrfutil keys display --key pk --format code priv.pem --out_file dfu_public_key.c</code>     </p>
<p><code>priv.pem</code> 就是私钥, 名字可改,生成升级包时需要使用此私钥，必需妥善保管，否则将永远不能升级<br><code>dfu_public_key.c</code> 就是公钥</p>
<h2 id="1-3-生成bootloader-hex"><a href="#1-3-生成bootloader-hex" class="headerlink" title="1.3 生成bootloader hex"></a>1.3 生成bootloader hex</h2><p>将生成的公钥<code>dfu_public_key.c</code>覆盖<code>SDK根目录\examples\dfu</code>下的同名文件,并生成<code>bootloader.hex</code>     </p>
<h1 id="2-App"><a href="#2-App" class="headerlink" title="2. App"></a>2. App</h1><p>生成自己的应用程序，假如取名为: <code>app.hex</code>     </p>
<h1 id="3-settings"><a href="#3-settings" class="headerlink" title="3. settings"></a>3. settings</h1><p>生成setting    </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nrfutil settings generate --family NRF52 --application app.hex --application-version 1 --bootloader-version 1 --bl-settings-version 1 settings.hex</span><br></pre></td></tr></table></figure>

<h1 id="4-生成最终烧录Hex"><a href="#4-生成最终烧录Hex" class="headerlink" title="4. 生成最终烧录Hex"></a>4. 生成最终烧录Hex</h1><p>合并hex: setting, bootloader, softdevice, app    </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mergehex --merge bootloader.hex settings.hex --output bl_temp.hex </span><br><span class="line">mergehex --merge bl_temp.hex app.hex s132_nrf52_7.0.1_softdevice.hex --output whole.hex</span><br></pre></td></tr></table></figure>

<h1 id="5-生成升级zip包"><a href="#5-生成升级zip包" class="headerlink" title="5. 生成升级zip包"></a>5. 生成升级zip包</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nrfutil pkg generate --application app_new.hex --application-version 2 --hw-version 52 --sd-req 0xCB --key-file priv.pem APP_DFU_PACKET.zip</span><br></pre></td></tr></table></figure>
<p><code>--application</code>表示新固件hex文件<br><code>--key-file</code> 表示签名用的私钥文件<br><code>--sd-req</code>表示协议栈softdevice版本 可以通过命令<code>nrfutil pkg generate --help</code>获得列表    </p>
<h1 id="6-使用nRFConnect-DFU"><a href="#6-使用nRFConnect-DFU" class="headerlink" title="6. 使用nRFConnect DFU"></a>6. 使用nRFConnect DFU</h1><p>nRFConnect支持DFU功能，直接选择待升级的升级zip包<code>APP_DFU_PACKET.zip</code>即可完成升级.<br>具体流程见  <a href="%E5%BC%BA%E5%A4%A7%E7%9A%84%E8%BE%85%E5%8A%A9%E8%BD%AF%E4%BB%B6nRFConnect.md">强大的辅助软件nRFConnect</a>        </p>
<h1 id="7-手机端DFU"><a href="#7-手机端DFU" class="headerlink" title="7. 手机端DFU"></a>7. 手机端DFU</h1><p>官方提供Android 及 iOS平台的DFU库:     </p>
<ul>
<li>Android版DFU库：<a target="_blank" rel="noopener" href="https://github.com/NordicSemiconductor/Android-DFU-Library">https://github.com/NordicSemiconductor/Android-DFU-Library</a>     </li>
<li>iOS版DFU库：<a target="_blank" rel="noopener" href="https://github.com/NordicSemiconductor/IOS-Pods-DFU-Library">https://github.com/NordicSemiconductor/IOS-Pods-DFU-Library</a>     </li>
</ul>
<p>官方还提供一个开源app  <code>nRF Toolbox</code>,内集成多个功能，包含DFU:    </p>
<ul>
<li>Android版本 <a target="_blank" rel="noopener" href="https://github.com/NordicSemiconductor/Android-nRF-Toolbox">https://github.com/NordicSemiconductor/Android-nRF-Toolbox</a>    </li>
<li>iOS版本: <a target="_blank" rel="noopener" href="https://github.com/NordicSemiconductor/IOS-nRF-Toolbox">https://github.com/NordicSemiconductor/IOS-nRF-Toolbox</a>     </li>
</ul>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><ul>
<li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/iini/p/9314246.html">详解蓝牙空中升级(BLE OTA)原理与步骤</a> (非常详细，推荐)    </li>
<li><a target="_blank" rel="noopener" href="https://infocenter.nordicsemi.com/index.jsp?topic=/com.nordic.infocenter.sdk5.v15.3.0/lib_bootloader_dfu_banks.html&cp=7_5_0_3_5_1_2">Bootloader and DFU modules</a></li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/BLE/" rel="tag"><i class="fa fa-tag"></i> BLE</a>
              <a href="/tags/Nordic/" rel="tag"><i class="fa fa-tag"></i> Nordic</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/07/05/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-ble%E5%8F%91%E9%80%81%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B8%A2%E5%8C%85/" rel="prev" title="nRF52832开发记录-ble发送数据丢包">
      <i class="fa fa-chevron-left"></i> nRF52832开发记录-ble发送数据丢包
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/07/20/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-%E4%BB%A3%E7%A0%81%E8%AF%BB%E4%BF%9D%E6%8A%A4/" rel="next" title="nRF52832开发记录-代码读保护">
      nRF52832开发记录-代码读保护 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-Bootloader"><span class="nav-number">1.</span> <span class="nav-text">1. Bootloader</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-%E7%94%9F%E6%88%90micro-ecc%E7%AE%97%E6%B3%95%E5%BA%93"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 生成micro-ecc算法库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-%E7%94%9F%E6%88%90%E5%85%AC%E9%92%A5%E5%8F%8A%E7%A7%81%E9%92%A5"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 生成公钥及私钥</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-%E7%94%9F%E6%88%90bootloader-hex"><span class="nav-number">1.3.</span> <span class="nav-text">1.3 生成bootloader hex</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-App"><span class="nav-number">2.</span> <span class="nav-text">2. App</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-settings"><span class="nav-number">3.</span> <span class="nav-text">3. settings</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-%E7%94%9F%E6%88%90%E6%9C%80%E7%BB%88%E7%83%A7%E5%BD%95Hex"><span class="nav-number">4.</span> <span class="nav-text">4. 生成最终烧录Hex</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-%E7%94%9F%E6%88%90%E5%8D%87%E7%BA%A7zip%E5%8C%85"><span class="nav-number">5.</span> <span class="nav-text">5. 生成升级zip包</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6-%E4%BD%BF%E7%94%A8nRFConnect-DFU"><span class="nav-number">6.</span> <span class="nav-text">6. 使用nRFConnect DFU</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#7-%E6%89%8B%E6%9C%BA%E7%AB%AFDFU"><span class="nav-number">7.</span> <span class="nav-text">7. 手机端DFU</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number">8.</span> <span class="nav-text">参考</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">wusk</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">34</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">wusk</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
